import 'package:flutter/material.dart';
import '../../model/danger2.dart';
import '../../widgets/IconWidget.dart';
import '../../widgets/someCards.dart';
import '../../helper/Helper.dart';
import '../../helper/uidata.dart';

class Danger2AddPage extends StatefulWidget {
  @override
  Danger2AddPage(this.added);

  final VoidCallback added;
  @override
  Danger2AddPageState createState() => new Danger2AddPageState();
}

class Danger2AddPageState extends State<Danger2AddPage> {
  List<String> typeList = {"一般", "重要", "紧急"}.toList();
  late Danger2 X1;

  final TextEditingController c1 = new TextEditingController();
  final TextEditingController c2 = new TextEditingController();
  final TextEditingController c3 = new TextEditingController();
  final TextEditingController c4 = new TextEditingController();
  final TextEditingController c5 = new TextEditingController();
  final TextEditingController c6 = new TextEditingController();

  @override
  void initState() {
    // TODO: implement initState
    super.initState();

    var now = Helper.DateTimeNowString();
    var uGuid = UIData.CurrentUser!.Guid;

    X1 = Danger2(0, Helper.GetGuid(), "一般", "标题", "内容", "", "", uGuid, "处理结果",
        now, 0, now, now, uGuid, uGuid, "", now, "");

    c1.text = X1.Title;
    c2.text = X1.Content;

    //c3.text=X1.Process;
    //c4.text=X1.Workers;
  }

  upload(BuildContext context) async {
    X1.Title = c1.text;
    X1.Content = c2.text;
    // X1.Process=c3.text;
    // X1.Workers=c4.text;

    if (X1.ID == 0) {
      await Danger2.Add(X1);
    } else {
      await Danger2.Update(X1);
    }

    widget.added();
    Navigator.of(context).pop();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      //backgroundColor: Colors.white,
      appBar: AppBar(
        title: Text("隐患排查登记"),
      ),
      body: Danger2AddBody(context),
    );
  }

  Danger2AddBody(BuildContext context) => SingleChildScrollView(
        child: Danger2AddFields(context),
      );

  Danger2AddFields(BuildContext context) {
    return Container(
        child: Column(children: <Widget>[
      SomeCard.inCard(ListTile(
          leading: IconWidget.DangerImage(),
          title: Text(
            "隐患登记",
            style: TextStyle(fontWeight: FontWeight.bold),
          ))),
      SomeCard.inCard(
        ListTile(
            title: Text(
              "隐患标题",
              style: TextStyle(fontWeight: FontWeight.bold),
            ),
            subtitle: TextField(
              maxLines: 1,
              controller: c1,
            )),
      ),
      SomeCard.inCard(
        ListTile(
            title: Text(
              "内容简述",
              style: TextStyle(fontWeight: FontWeight.bold),
            ),
            subtitle: TextField(
              maxLines: null,
              controller: c2,
            )),
      ),
      SomeCard.inCard(ListTile(
        title: Center(
            child: Text(
          "保存",
          style: TextStyle(color: Colors.blue),
        )),
        onTap: () {
          upload(context);
        },
      )),
    ]));
  }
}
